/*
 * Step.java
 *
 * Created on 15 de abril de 2007, 18:20
 */

package servidor;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Pellizard
 */
public class Step {
    
    private String title;
    private String content;
   
    /**
     * Creates a new instance of Step
     */
    public Step( String titulo, String descripcion ) {
        
        this.title = titulo;
        this.content = descripcion;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String titulo) {
        this.title = titulo;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String descripcion) {
        this.content = descripcion;
    }
    
    public Node toXml(){
      try
      {
          DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
          DocumentBuilder parser = fact.newDocumentBuilder();
          Document doc = parser.newDocument();

          return toXml( doc );
      }
      catch( Exception ex )
      {
         return null;
      }
  }
    
    public Node toXml( Document doc ){
        
      try {
    
      Node root = doc.createElement("step");
      //doc.appendChild(root);

      Node titulo = doc.createElement("title");
      titulo.appendChild(doc.createTextNode(this.title));
      root.appendChild(titulo);
      
      Node contenido = doc.createElement("content");
      contenido.appendChild(doc.createTextNode(this.content));
      root.appendChild(contenido);
      
      return root;

    } catch (Exception ex) {
      System.err.println("+============================+");
      System.err.println("|        XML Error           |");
      System.err.println("+============================+");
      System.err.println(ex.getClass());
      System.err.println(ex.getMessage());
      System.err.println("+============================+");
      ex.printStackTrace();
      return null;
    }
  } 
    
    public static Step fromXml( String str )
  {
        ByteArrayInputStream is;
        try{
             is = new ByteArrayInputStream( str.getBytes("UTF8") );
        }catch(Exception e){
            return null;
        }
        
        try
        {
            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = fact.newDocumentBuilder();
            Document doc = parser.parse( is );
            
            return fromXml( doc );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }
    
    public static Step fromXml( Document doc )
  {
        String title = "", content = "";
      
        try
        {
            NodeList nodes = doc.getElementsByTagName( "title" );
            
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                title = node.getFirstChild().getNodeValue();
            }
            
            nodes = doc.getElementsByTagName( "content" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                content = node.getFirstChild().getNodeValue();
            }
            
            return new Step( title, content );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }

}
